#include <QtGui>
#include "mapQ20Dialog.h"

MapQ20Dialog::MapQ20Dialog(QWidget *parent) : QWidget(parent) {

//Кодек для руссификации
  russianCodec = QTextCodec::codecForName("UTF-8");
  QTextCodec::setCodecForTr(russianCodec);
  QTextCodec::setCodecForCStrings(russianCodec);

//Установки для окна
  this->setWindowTitle(tr("Карта"));
  // this->setFixedSize(WMAPQ20, HMAPQ20);
  this->resize(WMAPQ20, HMAPQ20);

  palette.setBrush(QPalette::Window, QBrush(QColor(0, 0, 0)));
  this->setAutoFillBackground(true);
  this->setPalette(palette);

  begDrawX = 0;
  begDrawY = 0;
  scaleX = 0.5;
  scaleY = 0.5;
  shiftX = 0;
  shiftY = 0;

  isPressLeftMouse = false;

  lastX = 0;
  lastY = 0;

} //MapQ20Dialog::MapQ20Dialog(QWidget *parent) : QWidget(parent)

void MapQ20Dialog::paintEvent(QPaintEvent *) {

  QPainter painter(this);
  draw(&painter);

} //void MapQ20Dialog::paintEvent(QPaintEvent *event)

void MapQ20Dialog::draw(QPainter * painter) {

  begDrawX = WMAPQ20 - (scaleX*WIMGMAP) - shiftX;
  begDrawY = HMAPQ20 - (scaleY*HIMGMAP) - shiftY;

  painter->scale(scaleX, scaleY);
  
  painter->drawImage(begDrawX, begDrawY, QImage(":/resources/q20Map.gif"));

} //void MapQ20Dialog::draw(QPainter * painter)

void MapQ20Dialog::wheelEvent(QWheelEvent * event) {

  if (event->delta() > 0) {

    // shiftX = int(begDrawX - int((scaleX*WIMGMAP)/2));
    // shiftY = int(begDrawY - int((scaleY*HIMGMAP)/2));

    // shiftX -= int((begDrawX + int((scaleX*WIMGMAP)/2) - event->x())*(1-scaleX));
    // shiftY -= int((begDrawY + int((scaleY*HIMGMAP)/2) - event->y())*(1-scaleY));

    if (scaleX <= 2.5)
      scaleX += 0.1;

    if (scaleY <= 2.5)
      scaleY += 0.1;

    update();

  } //if (event->delta() > 0)

  else {

    // shiftX = int(begDrawX + int((scaleX*WIMGMAP)/2));
    // shiftY = int(begDrawY + int((scaleY*HIMGMAP)/2));

    // shiftX += int((begDrawX + int((scaleX*WIMGMAP)/2) - event->x())*(1-scaleX));
    // shiftY += int((begDrawY + int((scaleY*HIMGMAP)/2) - event->y())*(1-scaleY));

    if (scaleX >= 0)
      scaleX -= 0.1;

    if (scaleY >= 0)
      scaleY -= 0.1;

    update();

  } //else: if (event->delta() > 0)

} //void MapQ20Dialog::wheelEvent(QWheelEvent * event)

void MapQ20Dialog::mousePressEvent(QMouseEvent * event) {

  if (event->buttons() == Qt::LeftButton) {

    isPressLeftMouse = true;

    lastX = event->x();
    lastY = event->y();

    update();

  } //if (event->buttons() == Qt::XButton1)

  else if (event->buttons() == Qt::RightButton) {

    shiftX -= int((begDrawX + int((scaleX*WIMGMAP)/2) - event->x())*(1-scaleX));
    shiftY -= int((begDrawY + int((scaleY*HIMGMAP)/2) - event->y())*(1-scaleY));

    scaleX += 0.025;
    scaleY += 0.025;

    update();

  } //else if (event->buttons() == Qt::XButton2)

} //void MapQ20Dialog::mousePressEvent(QMouseEvent * event)

void MapQ20Dialog::mouseReleaseEvent(QMouseEvent * ) {

  isPressLeftMouse = false;
  
} //void MapQ20Dialog::mouseReleaseEvent(QMouseEvent * event)

void MapQ20Dialog::mouseMoveEvent(QMouseEvent * event) {

  if (isPressLeftMouse) {
  
    shiftX += (lastX - event->x());
    shiftY += (lastY - event->y());

    lastX = event->x();
    lastY = event->y();

    update();

  } //if (isPressLeftMouse)

} //void MapQ20Dialog::mouseMoveEvent(QMouseEvent * event)

void MapQ20Dialog::keyPressEvent(QKeyEvent * event) {

  if (event->key() == Qt::Key_Escape) {

    this->hide();

  } //else if (event->key() == Qt::Key_Return)

} //void MapQ20Dialog::keyPressEvent(QKeyEvent * event)
